package org.soderiakelvy.model.dao.imp;

import java.io.Serializable;
import java.util.List;



import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.soderiakelvy.model.dao.SujetoDao;
import org.soderiakelvy.model.domain.SucursalVO;
import org.soderiakelvy.model.domain.SujetoVO;
import org.soderiakelvy.model.domain.TipoClienteVO;
import org.soderiakelvy.model.domain.TipoProductoVO;
import org.soderiakelvy.model.util.HibernateUtil;

public class SujetoDaoImp implements SujetoDao, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Deprecated
	public List<SujetoVO> buscarSujetos(SujetoVO sujeto, String condicion) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(SujetoVO.class);
//		if(condicion.equals("SUJETO_ID"))
//			criteria.add(Restrictions.eq("sujetoId", sujeto.getSujetoId()));
//		else if(condicion.equals("APELLIDO"))
//			criteria.add(Restrictions.like("apellido", sujeto.getApellido()));
//		criteria.addOrder(Order.asc("sujetoId"));
		List<SujetoVO> sujetos = criteria.list();
		session.close();
		return sujetos;
	}

	@Override
	public void insertSujetoVO(SujetoVO sujeto) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(sujeto);
		session.getTransaction().commit();
		session.close();
	}

	@Override
	public void deleteSujevoVO(SujetoVO sujeto) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(sujeto);
		session.getTransaction().commit();
		session.close();
		
	}

	@Override
	public void updateSujetoVO(SujetoVO sujeto) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(sujeto);
		session.getTransaction().commit();
		session.close();
	}

	@Override
	public SujetoVO getSujetoVO(SujetoVO sujeto) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(SujetoVO.class);
		criteria.add(Restrictions.eq("sujetoId", sujeto.getSujetoId()));
		SujetoVO s = (SujetoVO)criteria.uniqueResult();
		session.close();
		return s;
	}

	@Override
	public List<SujetoVO> getSujetoByTipoCliente(TipoClienteVO tipoCliente){
		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(SujetoVO.class);
			criteria.add(Restrictions.eq("tipoCliente", tipoCliente));
			List<SujetoVO> result = criteria.list();
			session.close();
			return result;	
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		
	}
}
